Page({
  data: {
    proObj: {},
    num: 0,
    addflage: 1,
    favoriteStatus: "收藏商品",
  },
  onLoad: function (options) {
    this.setData({
      skuId: options.skuid
    })
  },
  onShow: function () {
    this.getDetailsInfo();
    getApp().getCartNum(this);
  },
  getDetailsInfo() {
    var url = getApp().data.url + 'product/getProduct';
    var body = {
      supermarket_id: wx.getStorageSync('supermarket_id'),
      sku: this.data.skuId
    };
    getApp().wxAjax(this.splitDetails, url, body);
  },
  splitDetails(res) {
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      var pDetailsObj = resObj.body;
      this.setData({
        proObj: pDetailsObj
      })
      if (pDetailsObj.is_focus) {
        this.setData({
          favoriteStatus: "已收藏"
        })
      } else {
        this.setData({
          favoriteStatus: "收藏商品"
        })
      }

      console.log(pDetailsObj)
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  tofollow() {
    if (this.data.favoriteStatus == '收藏商品')
      this.tofocus()
    else
      this.tounfocus()
  },
  tofocus() {
    var url = getApp().data.url + 'product/addAttention';
    var body = {
      supermarket_id: wx.getStorageSync('supermarket_id'),
      product_id: this.data.proObj.prod_id
    };
    getApp().wxAjax(this.echofocus, url, body);
  },
  echofocus(res) {
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      this.setData({
        favoriteStatus: "已收藏"
      })
      wx.showToast({
        title: '收藏成功',
        icon: 'none'
      })
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  tounfocus() {
    var url = getApp().data.url + 'product/cancelAttention';
    var body = {
      supermarket_id: wx.getStorageSync('supermarket_id'),
      product_id: this.data.proObj.prod_id
    };
    getApp().wxAjax(this.echounfocus, url, body);
  },
  echounfocus(res) {
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      this.setData({
        favoriteStatus: "收藏商品"
      })
      wx.showToast({
        title: '已取消收藏',
        icon: 'none'
      })
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  addPro() {
    var url = getApp().data.url + 'shoppingcart/addShoppingCart';
    var body = {
      quantity: '1',
      supermarket_id: wx.getStorageSync('supermarket_id'),
      sku: this.data.proObj.sku
    };
    if (this.data.addflage == 1) {
      this.setData({
        addflage: 2
      })
      getApp().wxAjax(this.addProSug, url, body);
    }

  },
  addProSug(res) {
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      this.setData({
        addflage: 1
      })
      wx.showToast({
        title: '添加成功',
      })
      getApp().getCartNum(this);
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  tocart() {
    wx.navigateTo({
      url: '../cart/cart'
    })
  },

})